package org.jugile.jsontmpl.domain;

import java.util.HashMap;
import java.util.Map;
import org.jugile.util.IDomain;
import org.jugile.util.Node;
import org.jugile.jsontmpl.common.*;
import org.jugile.jsontmpl.domain.DomainBase;

public class Domain extends DomainBase implements IDomain {

	public Obj createObj(String name) {
		return createObj().setName(name);
	}
	
	public Obj createObj(Ctx ctx, Node n) {
		return createObj().parse(ctx,n);
	}

	public Obj getRootObj(String fname) {
		//return getObjs().byFname(fname).unique(); // TODO should be unique 
		return getObjs().byFname(fname).first("id");
	}
	
	public Obj parseXmlFile(Ctx ctx, String fname) {
		fname = ctx.getFilenameWithPath(fname);
		if (!ctx.push(fname)) return null; // check cycles by pushing fname to stack
		Node n = parseXml(fname);
		if (!FldType.OBJ.getLabel().equals(n.name())) fail("root element must be obj");
		Obj o = createObj(ctx,n).setFname(fname);
		ctx.pop(); // pop fname from stack
		return o;
	}
	
	public Array2 createEmptyArray() {
		return createArray2();
	}

	public Array2 createArray(Ctx ctx, Node n) {
		return createEmptyArray().parse(ctx, n);
	}

	public FldType getFldType(String tagname) {
		for (FldType ft : FldType.getItems()) {
			if (ft.getLabel().equals(tagname)) return ft;
		}
		return null;
	}

	public void setReadTx() {
		// TODO Auto-generated method stub
		
	}

	public void setWriteTx() {
		// TODO Auto-generated method stub
		
	}


}